import { useConfigStore } from '@/stores/config'

export const getCurrency = (currency) => {

  const currencyForm = useConfigStore().getHBDWCode

  if (currency) {
    const obj = currencyForm.find((item) => item.id === Number(currency))
    const obj2 = currencyForm.find((item) => item.code === currency)
    if (obj) return obj
    else if (obj2) return obj2
    else
      return {
        symbol: null,
        name: null,
        id: -1,
      }
  } else
    return {
      symbol: null,
      name: null,
      id: -1,
    }
}

export const formatPrice = (price) => {
  if (price > 1000) {
    return price / 1000 + 'k'
  } else {
    return price
  }
}

// 格式化价格
export function padZeroPrice(price) {
  // 如果价格为空，返回0.00
  if (price === null || price === undefined || price === '') {
    return '0.00'
  }

  // 将价格转换为浮动数字，并保证两位小数
  const formattedPrice = parseFloat(price).toFixed(2)

  // 返回格式化后的价格
  return formattedPrice
}

// 解析商品价格区间(产业链商品)
export function findMarketPriceRange(data, type) {
  let maxMarketPrice = -Infinity // 初始化最大值
  let minMarketPrice = Infinity // 初始化最小值
  // 遍历输入数组
  data?.forEach((item) => {
    if (item.goodsChannelSkuPriceList && Array.isArray(item.goodsChannelSkuPriceList)) {
      // 遍历 goodsChannelSkuPriceList
      item.goodsChannelSkuPriceList.forEach((priceItem) => {
        // 检查 type 是否为 1
        if (priceItem.type === type) {
          const marketPrice = priceItem.marketPrice

          // 更新最大值和最小值
          if (marketPrice > maxMarketPrice) {
            maxMarketPrice = marketPrice
          }
          if (marketPrice < minMarketPrice) {
            minMarketPrice = marketPrice
          }
        }
      })
    }
  })
  return {
    maxPrice: maxMarketPrice,
    minPrice: minMarketPrice,
    flag: maxMarketPrice === minMarketPrice,
  }
}
